﻿// ---------------------------------------------------------------------------------------------
// <copyright file="Component.cs" company="Hiyoko">
//   Copyright (c) 2015 All Rights Reserved
// </copyright>
// <summary>
//   This class represents a component - used as output.
// </summary>
// ---------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;

namespace Hiyoko.DependenciesSorter.CycleDetection
{
    public class Component
    {
        #region Properties

        public string Name { get; private set; }
        public List<string> Dependencies { get; private set; }

        #endregion Properties

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the Component class.
        /// </summary>
        /// <param name="vertex">The vertex</param>
        public Component(Vertex<string> vertex)
        {
            if (vertex == null)
            {
                throw new ArgumentException("An error occured: the argument <vertex> is null.");
            }

            Name = vertex.Value;
            Dependencies = new List<string>();

            foreach (var dependency in vertex.Dependencies)
            {
                if (!Dependencies.Contains(dependency.Value))
                {
                    Dependencies.Add(dependency.Value);
                }
            }
        }

        #endregion Constructor

        #region Public methods

        /// <summary>
        /// Adds a dependency
        /// </summary>
        /// <param name="name">The dependency name</param>
        public void AddDependency(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("An error occured: the argument <name> is null or empty.");
            }

            if (!Dependencies.Contains(name))
            {
                Dependencies.Add(name);
            }
        }

        /// <summary>
        /// Removes a dependency
        /// </summary>
        /// <param name="name">The dependency name</param>
        public void RemoveDependency(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("An error occured: the argument <name> is null or empty.");
            }

            if (Dependencies.Contains(name))
            {
                Dependencies.Remove(name);
            }
        }

        /// <summary>
        /// Displays object
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            var sb = new StringBuilder();
            sb.AppendLine("\t" + Name);
            foreach (var dependency in Dependencies)
            {
                sb.AppendLine("\t\t" + dependency);
            }
            return sb.ToString();
        }

        #endregion Public methods
    }
}
